//
//  MyShopViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/25.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "MyShopViewController.h"
#import "AddGoodsViewController.h"
#import "EditeMyShopViewController.h"
#import "GoodsManagerViewController.h"

@interface MyShopViewController ()
{

    NSData * imageData;
}
@property (weak, nonatomic) IBOutlet UIButton *companyImage;
@property (weak, nonatomic) IBOutlet UILabel *companyName;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *className;
@end

@implementation MyShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (self.dataDic) {
        
        
        [_companyImage sd_setBackgroundImageWithURL:ImageUrl(self.dataDic[@"uca"]) forState:UIControlStateNormal  placeholderImage:ImageName(@"icon_products_add")] ;
        _companyName.text = self.dataDic[@"ucn"];
        _addressLabel.text = self.dataDic[@"ucarea"];
        
        
        [CLASS_ID_ARRAY enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isEqualToString:self.dataDic[@"ucid"]]) {
                NSLog(@"%@-索引%d 对应id = %@",obj, (int)idx,CLASS_ID_ARRAY[idx]);
                _className.text =  CLASS_ARRAY[idx];
            }
        }];
}
    
    TitleView(@"我的商铺");
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithIcon:@"icon_nav_edit" target:self action:@selector(editBtn:)];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma markTest
- (IBAction)companyBtnClick:(id)sender {
    
    
    if ([USER_DEFAULTGet(@"ujd") isEqualToString:@"4"]) {
        [BDImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) {
            if (image) {
                
                [_companyImage setBackgroundImage:image forState:UIControlStateNormal];
                
                CGFloat fixelW = CGImageGetWidth(image.CGImage);
                CGFloat fixelH = CGImageGetHeight(image.CGImage);
                
                NSString * widthStr =   [NSString stringWithFormat:@"%f",fixelW ];
                NSString * heightStr =   [NSString stringWithFormat:@"%f",fixelH ];
                imageData = UIImageJPEGRepresentation(image, 0.4);
                
                [self upIconImageRequestFor:imageData forSize:widthStr andHeight:heightStr];
            }
        }];

    }else{
        SHuiMBhudTextTimeStr(@"您还不是认证会员！");
        return;

    }
 }

-(void)editBtn:(UIButton *)btn{
    
    EditeMyShopViewController * edit =[[EditeMyShopViewController alloc]init];
    edit.dataDic = self.dataDic;
    [self.navigationController pushViewController:edit animated:YES];

}
#warning testUpDataImage
- (IBAction)addGoodsClick:(id)sender {
    
    if ([USER_DEFAULTGet(@"ujd") isEqualToString:@"4"]){
    
        AddGoodsViewController * add =[[AddGoodsViewController alloc]init];
        [self.navigationController pushViewController:add animated:YES];
    }else{
        SHuiMBhudTextTimeStr(@"您还不是认证会员！");
        return;
    }

}
- (IBAction)goodsManager:(id)sender {
    
    GoodsManagerViewController *  goods =[[GoodsManagerViewController alloc]init];
    
    [self.navigationController pushViewController:goods animated:YES];
}

-(void)upIconImageRequestFor:(NSData*)data forSize:(NSString *)flowid andHeight:(NSString *)heig{
    
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];

//    NSString * sizeStr = [NSString stringWithFormat:@"%.f",700.0];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@?uphone=%@&app=phone&ver=%@&x1=0&x2=%@&y1=0&y2=%@&sw=%@&sh=%@",address_url,upData_image_api,USER_DEFAULTGet(@"phone"),md5ver,flowid,heig,flowid,heig];
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    SHuiMBhubNoTimeStr(@"正在提交...");
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData){
    
        NSString *fileName = @"123456.jpg";
        [formData appendPartWithFileData:data name:@"Filename" fileName:fileName mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //打印下上传进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            
            
            SHuiMBhudTextTimeStr(@"提交成功");
            
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                
//                [self.navigationController popViewControllerAnimated:YES];
//            });
            
        }else{
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        
        SHuiMBHidleYes;
        
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
        
    } ];
    
}



@end
